package com.fowo.api.syncLingxingApi.service.syncImpl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.fowo.api.entity.Supplier;
import com.fowo.api.syncLingxingApi.entity.TmpSyncLingxingToSupplier;
import com.fowo.api.syncLingxingApi.service.SyncLxApiBaseService;
import com.fowo.api.syncLingxingApi.service.impl.TmpSyncLingxingToSuplierServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
@DS("ds24")
public class SyncLxSupplierServiceImpl
        extends SyncLxApiBaseService <TmpSyncLingxingToSupplier, Supplier> {
    private final static String LX_API_URL = "supplierUrl";
    private final static int MAX_LEN = 1000;

    @Resource
    TmpSyncLingxingToSuplierServiceImpl tmpService;

    public SyncLxSupplierServiceImpl() {
        super(LX_API_URL, MAX_LEN);
    }

    @Override
    protected JSONObject pullLingxingData(long offset, long length, JSONObject params) {
        Map<String, Object> reqBody =new HashMap<>();
        reqBody.put("offset", offset);
        reqBody.put("length", length);
        return this.tryRequestLingxingApi(reqBody);
    }

    @Override
    protected boolean cmpNewAndOldEqualCond(TmpSyncLingxingToSupplier newOne, Supplier oldOne) {
        if (newOne == null || oldOne == null) {
            return false;
        }
//        String newSupplierId = newOne.getSupplierCode() == null? "": newOne.getSupplierCode();
//        String oldSupplierId = oldOne.getSupplierCode() == null? "": oldOne.getSupplierCode();
//        return newSupplierId.equals(oldSupplierId);
        return true;
    }

    @Override
    protected TmpSyncLingxingToSupplier cmpNewAndOldInsertProc(TmpSyncLingxingToSupplier newOne) {
        newOne.set_flag(2);
        newOne.set_version(this.data_version);
        return newOne;
    }

    @Override
    protected TmpSyncLingxingToSupplier cmpNewAndOldUpdateProc(TmpSyncLingxingToSupplier newOne, Supplier oldOne) {
        newOne.set_flag(1);
        newOne.set_version(this.data_version);
        return newOne;
    }

    @Override
    protected boolean cmpNewAndOldUpdateCond(TmpSyncLingxingToSupplier newOne, Supplier oldOne) {
//        if (newOne == null || oldOne == null) return false;
        return true;
    }

    @Override
    protected void saveTempToFormal() {
        log.info(">> update size is " + tmpService.updateToSupplier(this.data_version));
        log.info("<< insert size is " + tmpService.insertToSupplier(this.data_version));
    }

    @Override
    protected List<TmpSyncLingxingToSupplier> jsonDumpsResult(JSONArray jsonArray) {
        List<TmpSyncLingxingToSupplier> results = new ArrayList<>();

        for (Object obj: jsonArray) {
            JSONObject item = (JSONObject) obj;
            TmpSyncLingxingToSupplier supplier = new TmpSyncLingxingToSupplier();
            supplier.set_version(this.data_version);
            supplier.setSupplierId(item.getInteger("supplier_id"));
            supplier.setSupplierName(item.getString("supplier_name"));
            supplier.setSupplierCode(item.getString("supplier_code"));
            supplier.setEmployees(item.getInteger("employees"));
            supplier.setUrl(item.getString("url"));
            supplier.setContactPerson(item.getString("contact_person"));
            supplier.setContactNumber(item.getString("contact_number"));
            supplier.setQq(item.getString("qq"));
            supplier.setEmail(item.getString("email"));
            supplier.setFax(item.getString("fax"));
            supplier.setAccountName(item.getString("account_name"));
            supplier.setOpenBack(item.getString("open_bank"));
            supplier.setBankCardNumber(item.getString("bank_card_number"));
            supplier.setRemark(item.getString("remark"));
            supplier.setPurchaser(item.getString("purchase"));
            supplier.setIsDelete(item.getInteger("is_delete"));
            supplier.setAddressFull(item.getString("address_full"));
            supplier.setPaymentMethodText(item.getString("payment_method_text"));
            supplier.setPcName(item.getString("pc_name"));
            supplier.setSettlementMethodText(item.getString("settlement_method_text"));
            supplier.setEmployees_text(item.getString("employees_text"));
            supplier.setLevelText(item.getString("level_text"));
            supplier.setTotal(item.getString("total"));
            results.add(supplier);
        }

        return results;
    }

    @Override
    protected List<Supplier> getOldData(List<TmpSyncLingxingToSupplier> newObjects) {
        return tmpService.getSupplierByCode(newObjects);
    }

    @Override
    protected void saveData2TmpTb(List<TmpSyncLingxingToSupplier> results) {
        tmpService.saveBatch(results);

    }
}
